module service.FriendService;

import repository;
import model;
import client.MsgServerClient;
import msgtrans;
import hunt.logging;
import message.Constants;
import message.MsgDefine;
import std.conv : to;
import std.stdio;

class FriendService {

    static ChatMap processChatMap(int index, int size)
    {
        ChatMap mp = new ChatMap;

        auto friendRepo = new FriendRepository();
        auto userRepo = new UserRepository;

        Friend[] friends = friendRepo.findAll();
        foreach(friend ; friends)
        {
            if (friend.chatId % size == index)
            {
                GroupInfo cm = new GroupInfo;
                cm.groupId = friend.chatId;
                cm.memberId ~= friend.userId;
                cm.memberId ~= friend.friendId;
                mp.map ~= cm;
            }
            else
            {
                logWarningf("ChatMaping chat id: %s not index: %d", friend.id, index);
                std.stdio.stdout.flush();
            }
        }
        auto groupRepo = new GroupRepository();
        ChatGroup[] groups = groupRepo.getAll();

        foreach(ChatGroup group ; groups)
        {
             if (group.id % size == index)
             {
                 GroupInfo cm = new GroupInfo;
                 cm.groupId = group.id;
                 auto groupUserRepo = new GroupUserRepository();
                 GroupUser[] users =    groupUserRepo.findAllByID(group.id);
                 foreach (GroupUser user ; users)
                 {
                        cm.memberId ~= user.userId;
                 }
                 mp.map ~= cm;
             }
            else
            {
                logWarningf("ChatMaping group id: %s not index: %d", group.id, index);
                std.stdio.stdout.flush();
            }
        }

        auto onlineUser = userRepo.findAllOnline();
        foreach(int id ; onlineUser)
        {
            if (id % size == index)
            {
                mp.onlineMember ~= id;
            }
            else
            {
                logWarningf("ChatMaping online user id: %s not index: %d", id, index);
            }
        }
        return mp;
    }

    static void processDeleteFriend(string userId,string friendId)
    {
            auto friendRep = new FriendRepository();
            friendRep.deleteFriend(userId, friendId);
    }

    static void setMsgRead(string userId, string chatId, long timestamp)
    {
        auto friendRep = new FriendRepository();
        friendRep.updateMsgRead(userId,chatId,timestamp);
    }

    static bool setContactAliasName(string userId, string chatId, string aliasName)
    {
        auto friendRep = new FriendRepository();
        return friendRep.setAliasName(userId,chatId,aliasName);
    }

    static bool isSelf(string chatId, string userId)
    {
        auto friendRep = new FriendRepository();
        Friend[] friend = friendRep.findAllBychatId(to!int(chatId));
        if(friend.length != 0 && friend[0].userId == to!uint(userId) && friend[0].friendId == to!uint(userId))
        {
            return true;
        }
        return false;
    }
}

